#define  _CRT_SECURE_NO_WARNINGS 1	
#include "SList.h"

// 动态申请一个节点
SListNode* BuySListNode(SLTDateType x)
{
	SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
	if (newnode == NULL)
	{
		perror("malloc");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;
}

// 单链表打印
void SListPrint(SListNode* plist)
{
	SListNode* cur = plist;
	while (cur)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

// 单链表尾插
void SListPushBack(SListNode** pplist, SLTDateType x)
{
	SListNode* newnode = BuySListNode(x);
   
	if (*pplist == NULL)
	{
		*pplist = newnode;
	}
	else
	{
		SListNode* tail = *pplist;
		while (tail->next)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}

// 单链表的头插
void SListPushFront(SListNode** pplist, SLTDateType x)
{
	SListNode* newnode = BuySListNode(x);
	newnode->next = *pplist;
	*pplist = newnode;
}

// 单链表的尾删
void SListPopBack(SListNode** pplist)
{
	assert(*pplist);

	SListNode* pre = NULL;
	SListNode* tail = *pplist;

	if ((*pplist)->next == NULL)
	{
		*pplist = NULL;
	}
	else
	{
	   /* while (tail->next)
	   {
		 pre = tail; 
		 tail = tail->next;
	   }
	     free(tail);
	     tail = NULL;
	     pre->next = NULL;*/
		SListNode* tail = *pplist;
		while (tail->next->next)
		{
			tail = tail->next;
		}
		free(tail->next);
		tail->next = NULL;
	} 
}

void SListPopFront(SListNode** pplist)
{
	assert(pplist);

	if ((*pplist)->next == NULL)
	{
		*pplist = NULL;
	}
	else
	{
		SListNode* ret = ((*pplist)->next);
		free(*pplist);
		*pplist = ret;
	}
}

SListNode* SListFind(SListNode* plist, SLTDateType x)
{
	assert(plist);

	SListNode* ret = plist;
	while (ret->data != x&&ret->next)
	{
		ret=ret->next;
	}
	if (ret->next == NULL && ret->data != x)
		return NULL;
	else
		return ret; 
}

// 单链表在pos位置之后插入x
void SListInsertAfter(SListNode** pphead,SListNode* pos, SLTDateType x)
{
	assert(pphead);
	//检测插入位置是否存在
	assert(pos); 
	assert(*pphead);

	SListNode* newnode=BuySListNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

// 单链表删除pos位置之后的值
void SListEraseAfter(SListNode** pphead,SListNode* pos)
{
	assert(pphead);
	assert(pos);
	assert(pos->next);
	assert(*pphead);

	SListNode* tmp = pos->next;
	pos->next = pos->next->next;
	free(tmp);
	tmp = NULL;
}

SListNode* SListInsertFront(SListNode** pphead, SListNode* pos, SLTDateType x)
{
	assert(pphead);
	assert(pos);
	assert(*pphead);

	SListNode* newnode = BuySListNode(x);
	SListNode* pre = *pphead;

	if (*pphead == pos)
	{
		SListPushFront(pphead,x);
	}
	else
	{
		while (pre->next != pos)
		{
			pre = pre->next;
		}
		newnode->next = pos;
		pre->next = newnode;
	}
	

}
void SLTErase(SListNode** pphead, SListNode* pos)
{
	assert(pphead);
	assert(pos);
	assert(*pphead);
	if (*pphead == pos)
	{
		SListPopFront(pphead);
	}
	else
	{
		SListNode* pre = *pphead;
		while (pre->next != pos)
		{
			pre = pre->next;
		}
		pre->next = pos->next;
		free(pos);
		pos = NULL;
	}

}

void SLTDestroy(SListNode** pphead)
{
	SListNode* cur = *pphead;
	SListNode* ret = NULL;

	while (cur)
	{
		ret = cur->next;
		free(cur);
		cur = ret;
	}
	*pphead = NULL;
}

